package com.estar.assistsurvy.activity;

import java.util.List;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.BankCardListviewAdapter;
import com.estar.assistsurvy.entity.EsBankCardInfo;
import com.estar.assistsurvy.entity.RequestWalletChange;
import com.estar.assistsurvy.entity.ResponseBankCardInfo;
import com.estar.assistsurvy.thread.BankInfoThread;
import com.estar.assistsurvy.utils.ExitUtils;
import com.estar.assistsurvy.utils.MightypleUtil;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class ShowBankCardActivity extends BaseActivity implements OnClickListener{

	private LinearLayout returnBack;
	private RelativeLayout addBankCard;
	private ListView bankCardList;
	private SharedPreferences prefer;
	private String uuid,deviceId;
	private List<EsBankCardInfo> esBankCardInfoList;
	private BankCardListviewAdapter adapter;
	private Intent intent;
	private ProgressDialog dialog;
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(dialog!=null){
				dialog.dismiss();
			}
			switch (msg.what) {
			case -1:
				showShortToast((String) msg.obj);
				break;
				
			case 0:
				ResponseBankCardInfo bankCard=new ResponseBankCardInfo();
				bankCard=(ResponseBankCardInfo) msg.obj;
				if(bankCard.getCode().equals("1")){
					esBankCardInfoList=bankCard.getEsBankCardInfoList();
					adapter=new BankCardListviewAdapter(esBankCardInfoList, ShowBankCardActivity.this);
					bankCardList.setAdapter(adapter);
				}else if(bankCard.getCode().equals("0")){
					showShortToast(bankCard.getMessages());
				}else if(bankCard.getCode().equals("2")){
					ExitUtils.Unlogin(ShowBankCardActivity.this, bankCard.getMessages());
				}
				break;

			default:
				break;
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_showbankcard);
		initView();
	}
	
	private void initView(){
		returnBack=find(R.id.returnBack);
		returnBack.setOnClickListener(this);
		addBankCard=find(R.id.addBankCard);
		addBankCard.setOnClickListener(this);
		bankCardList=find(R.id.bankCardList);
		
		prefer=getSharedPreferences("user", MODE_PRIVATE);
		uuid=prefer.getString("uuid", "");
		deviceId=MightypleUtil.getDeviceId(this);
		dialog=ProgressDialog.show(this, null, "获取银行卡…", false, false);

		RequestWalletChange bankInfo=new RequestWalletChange(deviceId,uuid);
		new BankInfoThread(this, handler, bankInfo).start();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.returnBack:
			finish();
			break;
			
		case R.id.addBankCard:
			intent=new Intent(ShowBankCardActivity.this, AddBankCardActivity.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}
}
